// WF1991 - San Antonio, UVa210, Concurrency Simulator
// 陈锋
#include <bits/stdc++.h>
#define _for(i,a,b) for( int i=(a); i<(int)(b); ++i)
using namespace std;
const int NN = 1000 + 4;
int N, Quantum, C[5], Var[26], IP[NN]; // IP[pid]是程序pid运行的当前行号。
string Stats[NN]; // 程序语句
bool locked;
deque<int> readyQ, blockQ;
void run(int pid) {
  int q = Quantum;
  while (q > 0) {
    const string& p = Stats[IP[pid]];
    switch (p[2]) {
    case '=':
      Var[p[0] - 'a'] = isdigit(p[5]) ? (p[4] - '0') * 10 + p[5] - '0' : p[4] - '0';
      q -= C[0];
      break;
    case 'i': // print
      cout << pid + 1 << ": " << Var[p[6] - 'a'] << endl;
      q -= C[1];
      break;
    case 'c': // lock
      if (locked) { blockQ.push(pid); return; }
      locked = true;
      q -= C[2];
      break;
    case 'l': // unlock
      locked = false;
      if (!blockQ.empty()) {
        int pid2 = blockQ.front(); blockQ.pop();
        readyQ.push_front(pid2);
      }
      q -= C[3];
      break;
    case 'd': // end
      return;
    }
    IP[pid]++;
  }
  readyQ.push_back(pid);
}

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  int T; cin >> T;
  while (T--) {
    cin >> N;
    _for(i, 0, 5) cin >> C[i];
    cin >> Quantum, fill_n(Var, 26, 0);
    int line = 0;
    _for(i, 0, N) {
      getline(cin, Stats[line++]);
      IP[i] = line - 1;
      while (Stats[line - 1][2] != 'd') getline(cin, Stats[line++]);
      readyQ.push_back(i);
    }
    locked = false;
    while (!readyQ.empty()) {
      int pid = readyQ.front(); 
      readyQ.pop_front(), run(pid);
    }
    if (T) cout << endl;
  }
  return 0;
}
/*
算法分析请参考: 《入门经典-第2版》例题6-1
*/
// Accepted 120ms 1714 C++5.3.0 2020-12-13 17:07:11 25842847
